<template>
  <div class="list">
    <div style="display: flex; margin-bottom: 30px">
      <el-breadcrumb separator="/" class="breadcrumb">
        <!-- <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item> -->
        <el-breadcrumb-item :to="{ path: '/member/list' }">会员服务</el-breadcrumb-item>
        <el-breadcrumb-item>会员充值</el-breadcrumb-item>
      </el-breadcrumb>
      
    </div>
    <div class="content">
       <div class="top-list">
        <div class="goods-item" v-for="(item,index) in goodsList" :key="index" :class="{'active':isSelect===index}" @click="toSelectPay(item,index)">
          <div class="item-top">
            <!-- <el-image
              style="width: 100%; height: 170px"
              :src="url"
              :fit="fit"></el-image> -->
              <div class="desc">
                <div class="title">{{ item.goodsName?item.goodsName:'--' }}</div>
                <div class="price"> <span style="color: red;font-size: 20px;">{{ item.price?item.price:'--' }}</span> 元/年</div>
              </div>
              <div class="tips">温馨提示:到期后不会自动续费</div>
            </div>
          </div>
       </div>
       <div class="member-privilege">
        <div class="title">会员特权:</div>
        <div class="privilege-list">
          <div class="privilege-item" v-for="(item,index) in description" :key="index ">
            <img
              style="width:32px; height: 32px;margin-right: 10px;"
              src="../../assets/images/member-icon.png"
              />
              {{ index+1 }}.{{ item }}
          </div>
        </div>
       </div>
       <div class="pay-way">
        <div class="title">支付方式:</div>
        <div class="select-way">
          <el-radio-group v-model="payway" @change="changePayWay(payway)">
            <el-radio :label="2">
              <div class="zfb">
                <img
                  style="width:25px; height: 25px;margin-right: 10px;"
                  src="../../assets/images/pay-icon.png"
                />
                支付宝付款
            </div>
            </el-radio>
            <el-radio :label="1">
              <div class="wx">
                <img
                  style="width:25px; height: 25px;margin-right: 10px;"
                  src="../../assets/images/wx-icon.png"
                />
                微信付款
            </div>
            </el-radio>
          </el-radio-group>
        </div>
       </div>

       <div class="bottom-content"> 
        <div class="left-qrcode" >
          <div class="qrcode" ref="qrCodeUrl" @click="toPay">

          </div>
          <div class="desc">
            <span>{{ paywayText }}</span><br>
            扫一扫继续支付
          </div>
          <div class="download">
            {{ downloadText }}
          </div>
        </div>
        <div class="right-list">
          <div class="pay-item" v-for="(item,index) in orderList" :key="index">
              <span>{{ item.enterpriseName?item.enterpriseName.slice(0,3)+'********':'--' }} </span>
              <span style="color: red;">{{ item.paymentTime?item.paymentTime.slice(0,10):'--' }}</span>
              购买了
              <span style="color: red;">{{ item.goodsName?item.goodsName:'--' }}</span>
          </div>
        </div>
       </div>
    </div>
    
  </div>
</template>

<script>
import { goodsList,getGoodsDetail,getQrCode,sumbitOrder,orderPay } from '@/api/member/member'
import { orderList } from '@/api/order/order'
import QRCode from 'qrcodejs2-fix'

export default {
  name: "recharge",
  data() {
    return {
      loading: true,
      isSelect:0,
      payway:2,
      paywayText:'打开手机支付宝',
      downloadText:'首次使用请下载手机支付宝',
      goodsId:'',
      goodsList:[],
      description:[],
      orderList:[],
      detailInfo:{}
    }
  },
  mounted() {
    // this.goodsId=this.$route.query.goodsId
    // this.isSelect=Number(this.$route.query.index)
    // this.initData()
    this.$nextTick(() => {
      this.goodsId=this.$route.query.goodsId
      this.isSelect=Number(this.$route.query.index)
      this.initData()
    })
  },
  methods: {
    initData() {
      goodsList().then(res => {
        this.goodsList=res.rows
        console.log('goodsList',this.goodsList)
      })
     this.getDetail(this.goodsId)
      orderList().then(res => {
       let arr= res.rows.filter(function(item,index) {
          return item.paymentStatus==2
        })
        this.orderList=arr.slice(0,10)
      })
      this.getGoodsQrCode()
    
     
    },
    getDetail(){
      this.description=[]
      getGoodsDetail(this.goodsId).then(res=>{
        this.detailInfo=res.data
        res.data.description.split('</p>').forEach(item=>{
          if(item!==''){
            this.description.push(item.slice(3))
          }
        })
      })
    },
    getGoodsQrCode(){
      getQrCode(Number(this.payway)).then(res=>{
        let text=res.data.tradeNo
        new QRCode(this.$refs.qrCodeUrl, {
            text: text, // 需要转换为二维码的内容
            width: 180,
            height: 180,
            colorDark: '#000000',
            colorLight: '#ffffff',
            correctLevel: QRCode.CorrectLevel.H
        })
      })
    },
    toSelectPay(item,index){
      this.detailInfo=item
      this.isSelect=index
      this.goodsId=item.goodsId
      this.getDetail()
    },
    changePayWay(payway){
      if(payway==2){
        this.paywayText='打开手机支付宝'
        this.downloadText='首次使用请下载手机支付宝'
      }else{
        this.paywayText='打开手机微信'
        this.downloadText='首次使用请下载手机微信'
      }
      this.$refs.qrCodeUrl.innerHTML = ""
      this.getGoodsQrCode()
    },
    // 提交订单 提交支付
    toPay(){

      let data={
        "enterpriseName": this.$store.state.user.realName,
        "goodsId": this.goodsId,
        "goodsName": this.detailInfo.goodsName,
        "paymentMethod": this.payway,
        "price":this.detailInfo.price
      }
      sumbitOrder(data).then(res=>{
        let payData={...data,"orderId":res.data}
        orderPay(payData).then(result=>{
          if(result.code==200){
            this.$message({
              type: 'success',
              message:'您已成功支付，感谢您的购买'
            });
          }else{
            this.$message({
              type: 'error',
              message:'支付失败，请重新购买'
            } );
          }
        })
      })
    }
  },
  
}
</script>

<style lang="scss" scoped>
.list {
  .breadcrumb {
    height: 21px;
    font-family: MicrosoftYaHei, sans-serif;
    font-size: 14px;
    font-weight: normal;
    line-height: 21px;
    letter-spacing: 0em;
    margin-left: 30px
  }

  .content {
    background: #FFFFFF;
    border-radius: 10px;
    opacity: 1;
    // width: 1380px;
    width: 100%; 
    min-height: 860px;
    padding: 20px 200px;
  }
}
.top-list{
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start; 
}
.top-list .goods-item{
  cursor: pointer;
  border: 1px solid #E2E2E2;
  height: 180px;
  box-shadow: 0px 6px 20px 0px rgba(0, 0, 0, 0.06);
  flex: 1;
  width: 320px;
  margin-right:calc((100% - 960px) / 3);
  margin-top: 30px;
  min-width: 320px; 
  max-width: 320px; 
  border-radius: 10px;
  &:nth-child(3n) { 
    margin-right: 0;
  }

  .desc{
    // width: 120px;
    height: 88px;
    margin-top: 20px;
    font-size: 16px;
    font-weight: 700;
    color: #333333;
    line-height: 30px;
    text-shadow: 0px 4px 16px 0px rgba(0, 0, 0, 0.06);
  }
  
}
.active{
  border: 2px solid #13B69F !important;
}

.member-privilege , .pay-way{
  text-align: left;
  margin-top: 70px;
  .title{
    font-size: 16px;
    color: #333333;
    font-weight: 700;
    
  }
}

.privilege-list{
  display: flex;
  margin-top: 20px;
}
.privilege-item{
  display: flex;
  align-items: center;
  min-width: 100px;
  height: 48px;
  border-radius: 4px;
  background: #EAFAF8;
  border: 1px solid #AFE7DF;
  font-size: 14px;
  color: #333333;
  padding: 0 18px;
  margin-left: 14px;
}
.select-way{
  margin-top: 40px;
}
.zfb,.wx{
  display: flex;
  justify-content: center;
  align-items: center;
  width: 200px;
  height: 48px;
  border-radius: 4px;
  font-size: 16px;
  color: #FFFFFF;
}
.zfb{
  background: #3D6CEE;
}
.wx{
  background: #48BB57;
}
::v-deep .el-radio__label{
  float: right;
  margin-top: -20px;
}
.bottom-content{
  display: flex;
  margin-top: 30px;
  margin-left: 25px;
  height: 300px;
  

  .left-qrcode{
    width: 190px;
    height: 235px;
    border: 1px solid #E2E2E2;
    margin-right: 100px;
    padding: 5px;
    line-height: 20px;
    .qrcode{
      width: 180px;
      height: 180px;
      cursor: pointer;
    }
    .desc{
      margin-top: 5px;
      font-size: 14px;
      color: #333333;
    }
    .download{
      font-size: 12px;
      margin-top: 20px;
      border-bottom: 1px solid #666;
    }
  }
  .right-list{
    display: flex;
    flex-wrap: wrap;
    text-align: left;
    height: 220px;
    flex: 1;
    background-color: #F6F6F6;
    padding: 24px 48px;
    margin-right: 100px;
    border-radius: 4px;
    .pay-item{
      height: 40px;
      width: 50%;
    }
  }
}



</style>
